import { defineStore } from 'pinia'
import {
  getOrderList,
  addOrder,
  updateOrder,
  deleteOrder
} from '@/api/service'

// 订单对象结构建议：
// {
//   id, orderNumber, incomeType, customerName, employeeId, employeeName, petName, serviceName, amount, actualAmount, transactionDate, paymentMethod, orderStatus, status, note, ...
// }
export const useOrderStore = defineStore('order', {
  state: () => ({
    orders: []
  }),
  actions: {
    async fetchOrders(params = {}) {
      const res = await getOrderList(params)
      this.orders = res.data?.data || []
      return res
    },
    async addOrder(order, mockOnly = false) {
      if (mockOnly) {
        this.orders.push(order)
        return { code: 200, data: order }
      }
      const res = await addOrder(order)
      this.orders.push(order)
      await this.fetchOrders()
      return res
    },
    async updateOrder(id, data) {
      // 后端接口需要整个订单对象，需自行组装
      const res = await updateOrder({ id, ...data })
      await this.fetchOrders()
      return res
    },
    async deleteOrder(id) {
      const res = await deleteOrder(id)
      await this.fetchOrders()
      return res
    },
    setOrders(orders) {
      this.orders = orders
    }
  }
}) 